﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace App.controls
{
    public partial class pager : System.Web.UI.UserControl
    {
        #region 数据绑定事件
        //public delegate void DataListPageChangedEventHandler(object sender, DataGridPageChangedEventArgs e);
        /**/
        /// <summary>
        /// 数据绑定事件
        /// </summary>
        public delegate void DataBindEventHandler();
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            // 在这里放置使用者程序代码以初始化网页
            if (!Page.IsPostBack)
            {
                txtGoPage.Attributes.Add("onclick", "this.select()");
                txtGoPage.Style.Add("text-align", "center");
            }
        }
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: 此为 ASP.NET Web Form 设计工具所需的调用。
            //
            this.PageIndexChanged += new GridViewPageEventHandler(OnPageIndexChanged);
            base.OnInit(e);
        }
        #region 变量定义
        /**/
        /// <summary>
        /// 换页事件
        /// </summary>
        private event GridViewPageEventHandler PageIndexChanged;
        /**/
        /// <summary>
        /// 绑定数据
        /// </summary>
        public event DataBindEventHandler BindData;

        #endregion
        //第一页
        protected void btnFirst_Click(object sender, EventArgs e)
        {
            int intNewPage = 1;
            this.PageIndexChanged(this, new GridViewPageEventArgs(intNewPage));
        }

        //上一页
        protected void btnPrev_Click(object sender, EventArgs e)
        {
            int intNewPage = Convert.ToInt32(lblCurrentIndex.Text) - 1;
            this.PageIndexChanged(this, new GridViewPageEventArgs(intNewPage));
        }
        //下一页
        protected void btnNext_Click(object sender, EventArgs e)
        {
            int intNewPage = Convert.ToInt32(lblCurrentIndex.Text) + 1;
            this.PageIndexChanged(this, new GridViewPageEventArgs(intNewPage));
        }
        //最后一页
        protected void btnLast_Click(object sender, EventArgs e)
        {
            int intNewPage = Convert.ToInt32(lblPageCount.Text);
            this.PageIndexChanged(this, new GridViewPageEventArgs(intNewPage));
        }
        #region 页面大小选择
        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            int intNewPage = 1;
            this.PageIndexChanged(this, new GridViewPageEventArgs(intNewPage));
        }
        #endregion
        protected void txtGoPage_TextChanged(object sender, EventArgs e)
        {
            try
            {
                int intNewPage = Convert.ToInt32(txtGoPage.Text);
                if (intNewPage > PageCount)
                    intNewPage = PageCount;
                if (intNewPage < 1)
                    intNewPage = 1;
                this.PageIndexChanged(this, new GridViewPageEventArgs(intNewPage));
            }
            catch
            {
                int intNewPage = Convert.ToInt32(lblCurrentIndex.Text);
                this.PageIndexChanged(this, new GridViewPageEventArgs(intNewPage));
            }
        }

        #region 设定link是否可以点选
        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            if (this.CurrentPageIndex <= 1)
            {
                btnFirst.Enabled = false;
                btnPrev.Enabled = false;
            }
            else
            {
                btnFirst.Enabled = true;
                btnPrev.Enabled = true;
            }
            if (this.CurrentPageIndex >= this.PageCount)
            {
                btnNext.Enabled = false;
                btnLast.Enabled = false;
            }
            else
            {
                btnNext.Enabled = true;
                btnLast.Enabled = true;
            }
            if (this.PageCount <= 1)
                txtGoPage.Enabled = false;
            else
                txtGoPage.Enabled = true;
            base.Render(writer);
        }
        #endregion

        #region 取得或设定当前页
        /**/
        /// <summary>
        /// 取得或设定当前页
        /// </summary>
        [Category("其它"), Description("当前页"), Browsable(false), DefaultValue(1)]
        public int CurrentPageIndex
        {
            get
            {
                return Convert.ToInt32(lblCurrentIndex.Text);
            }
            set
            {
                lblCurrentIndex.Text = value.ToString();
                txtGoPage.Text = lblCurrentIndex.Text;
            }
        }
        #endregion

        #region 页面大小
        [Category("页面大小"), Description("页面大小"), Browsable(false), DefaultValue(0)]
        public int PageSize
        {
            get
            {
                return Convert.ToInt32(this.ddlPageSize.SelectedValue);
            }
        }
        #endregion

        #region 取得或设定总记录数
        /**/
        /// <summary>
        /// 取得或设定总记录数
        /// </summary>
        [Category("分页"), Description("总记录数"), Browsable(false), DefaultValue(0)]
        public int RecordCount
        {
            get
            {
                return Convert.ToInt32(lblRecordCount.Text);
            }
            set
            {
                lblRecordCount.Text = value.ToString();
            }
        }
        #endregion

        #region 取得或设定总页数
        /**/
        /// <summary>
        /// 总页数
        /// </summary>
        [Category("总页数"), Description("总页数"), Browsable(false), DefaultValue(0)]
        public int PageCount
        {
            get
            {
                return this.RecordCount % PageSize == 0 ? RecordCount / PageSize : RecordCount / PageSize + 1;
            }
            set
            {
                lblPageCount.Text = value.ToString();
            }
        }
        #endregion

        #region 改变页码
        private void OnPageIndexChanged(object source, GridViewPageEventArgs e)
        {
            this.CurrentPageIndex = e.NewPageIndex;
            this.BindData();
        }
        #endregion
    }
}